package com.kecheng.action;

import com.kecheng.dao.KeywordDao;
import com.kecheng.pojo.Keyword;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 关键字回复的增删改查
 *
 * @author yunxin
 */
@Action(value = "keyword", results = {
        @Result(name = "findall", location = "/admin/weixin/allkeyword.jsp"),
        @Result(name = "add", location = "/admin/weixin/addKeyword.jsp"),
        @Result(name = "update", location = "/admin/weixin/updatekey.jsp"),
        @Result(name = "keyword", location = "/w/keyword.jsp"),
})
@AllowedMethods(value = {("regex:.*")})
public class KeywordAction extends BaseAction {

    private File file;
    private String fileFileName;
    private String fileContentType;
    private List<Keyword> keywords;
    @Autowired
    private KeywordDao keywordDao;
    private Keyword keyword;
    private int[] id;
    private int id1;

    public String findall() {
        keywords = keywordDao.findall();
        return "findall";
    }

    public String add() throws IOException {
        keyword.setImg("http://" + peizhi.getNetName() + "/" + compressFile(file, fileFileName, null));
        keywordDao.add(keyword);
        msg = "success";
        return "add";
    }

    public String deleteone() {
        Keyword keyword1 = keywordDao.findbyid(keyword.getId());
        keywordDao.delete(keyword1);
        return findall();
    }

    public String delete() {

        for (int i = 0; i < id.length; i++) {
            Keyword keyword1 = keywordDao.findbyid(id[i]);
            keywordDao.delete(keyword1);
        }

        return findall();
    }

    public String update() {

        keyword = keywordDao.findbyid(keyword.getId());

        return "update";
    }

    public String update1() throws IOException {

        if (file == null) {
            Keyword keyword1 = keywordDao.findbyid(keyword.getId());
            keyword.setImg(keyword1.getImg());
            keywordDao.update(keyword);
            return findall();
        } else {
            keyword.setImg("http://" + peizhi.getNetName() + "/" + compressFile(file, fileFileName, null));
            keywordDao.update(keyword);
            return findall();
        }

    }

    public String findbyid() {
        keyword = keywordDao.findbyid(id1);
        return "keyword";
    }

    public List<Keyword> getKeywords() {
        return keywords;
    }

    public void setKeywords(List<Keyword> keywords) {
        this.keywords = keywords;
    }

    public Keyword getKeyword() {
        return keyword;
    }

    public void setKeyword(Keyword keyword) {
        this.keyword = keyword;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    @Override
    public String getMsg() {
        return msg;
    }

    @Override
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int[] getId() {
        return id;
    }

    public void setId(int[] id) {
        this.id = id;
    }

    public int getId1() {
        return id1;
    }

    public void setId1(int id1) {
        this.id1 = id1;
    }

}
